// components/user-data-sex/user-data-sex.js
const app = getApp()
const userDao = require('../../dao/userDao')
import util from '../../utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    textarea: '',
    sexRange: '',
    multiArray: [
      ['18', '19',
        '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30',
        '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40',
        '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50',
        '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60'
      ],
      ['18', '19',
        '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30',
        '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40',
        '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50',
        '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60'
      ]
    ],
    multiIndex: [6, 11],
  },

  lifetimes: {
    ready() {
      this.setData({
        textarea: app.data.currentUser.sexIntroduction,
        sexRange: app.data.currentUser.sexRange,
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    //提交按钮
    bindFormSubmit(e) {
     let age =  this.data.multiArray[0][this.data.multiIndex[0]] + '-' + this.data.multiArray[1][this.data.multiIndex[1]]
      userDao.update(app.data.currentUser._id, {sexIntroduction: e.detail.value.textarea, sexRange: age, updateTime: new Date()})
      .then( res => {
        app.data.currentUser.sexIntroduction = e.detail.value.textarea
        app.data.currentUser.sexRange = age
        util.setCache(app.keys.currentUser, app.data.currentUser)

        wx.showToast({
          title: '修改成功',
        })
      }).catch(res => console.log(res))
    },

    //年龄范围选择
    bindMultiPickerChange: function (e) {
      this.setData({
        multiIndex: e.detail.value,
        sexRange: this.data.multiArray[0][e.detail.value[0]] + '-' + this.data.multiArray[1][e.detail.value[1]]
      })
    },

  }
})